<?php 
class AttendUtils {
  public static function getClassRate($myPdo, $enrol_id, $class_id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.enrolid = :v1 AND t.classid = :v2 AND t.bocanceled = 0';
    $cond_vals->v = array(':v1' => $enrol_id, ':v2' => $class_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'oldsys_enrolclass', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.Student = :v1';
    $cond_vals->v = array(':v1' => $enrol_id);
    $attendance_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'oldsys_attendance', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    return AttendUtils::_attdn($myPdo, $attendance_tmp, $enrolled_classes);
  }
  
  public static function getAllRate($myPdo, $enrol_id, $class_id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.enrolid = :v1 AND t.classid = :v2 AND t.bocanceled = 0 ';
    $cond_vals->v = array(':v1' => $enrol_id, ':v2' => $class_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'oldsys_enrolclass', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.Student = :v1';
    $cond_vals->v = array(':v1' => $enrol_id);
    $attendance_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'oldsys_attendance', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    return AttendUtils::_attdn($myPdo, $attendance_tmp, $enrolled_classes);
  }
  
  
  public static function _attdn($myPdo, $attendance_tmp, $enrolled_classes) {
    $attens = array();
    for($i=0; $i<count($attendance_tmp); $i++){
      for($j=0; $j<count($enrolled_classes); $j++){
        if ($attendance_tmp[$i]->enrolclassid == $enrolled_classes[$j]->id && $attendance_tmp[$i]->Student == $enrolled_classes[$j]->enrolid) {
          array_push($attens, $attendance_tmp[$i]);
        }
      }
    }
    
    $v1 = 0; $v2 = 0;
    foreach ($attens as &$att) {
      if ($att->Value == 'absent') {
        $v1++;
      } else if ($att->Value == 'present') {
        $v2++;
      }
      if ($att->Value1 == 'absent') {
        $v1++;
      } else if ($att->Value1 == 'present') {
        $v2++;
      }
      if ($att->Value2 == 'absent') {
        $v1++;
      } else if ($att->Value2 == 'present') {
        $v2++;
      }
      if ($att->Value3 == 'absent') {
        $v1++;
      } else if ($att->Value3 == 'present') {
        $v2++;
      }
      if ($att->Value4 == 'absent') {
        $v1++;
      } else if ($att->Value4 == 'present') {
        $v2++;
      }
      if ($att->Value5 == 'absent') {
        $v1++;
      } else if ($att->Value5 == 'present') {
        $v2++;
      }
    }
    
    if (count($attens) == 0) {
      return -1;
    }
    
    return (int)($v2 * 100 / ($v2 + $v1));
  }
}
?>
